<?php
/*回款计划*/
header("Content-Type: text/html;charset=UTF-8");
class Payment_plan extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
	}

	public function index($offset = 0) {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'payment_plan_model', 'planmodel' );
			$limit = 10;
			$total = $this->planmodel->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['payment_plans'] = $this->planmodel->list_payment_plans ( $limit, $offset );
			$current_total=count($data ['payment_plans']);
			$data['current_total']=$offset+$current_total;
			
			$config ['base_url'] = base_url () . 'index.php/payment_plan/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();
			
			$data ['page_title'] = '回款计划 — 昊美科技CRM系统';
			$data ['class4'] = 'cur';
			$data ['all_users'] = $this->get_all_users();
			$this->load->view ( 'payment_plan_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	public function find($id = '') { /*查看*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_plan.*,user.user_name as user_name')->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_plan.owner_id',$user_id);
		}
		$this->db->where('payment_plan.id',$id);
		$query = $this->db->get();
		$data['payment_plans'] = $query->row();
		
		/*获取回款计划期次*/
		$this->db->select ( '*' )->from ( 'payment_plan_stage' )->where ( 'payment_plan_id', $id )->order_by("id", "asc");
		$query = $this->db->get ();
		$data['stages'] = $query->result();
		
		$this->load->view ( 'payment_plan_view', $data );
	}

	public function check($name = ''){ /*搜索*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_plan.*,user.user_name as user_name')->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_plan.owner_id',$user_id);
		}
		$this->db->like('payment_plan.contract_name',urldecode($name));
		$query = $this->db->get();
		$data['payment_plans'] = $query->result();
		$data ['page_title'] = '回款计划 — 昊美科技CRM系统';
		$data ['class4'] = 'cur';
		$this->load->view ( 'payment_plan_list', $data );
	}
	
	/**
	 * 数据所有者查询
	 * */
	public function check_user_messages($id = ''){
		$data['payment_plans'] = $this->list_user_payment_plans($id);
		$data ['pag_links'] = '';
		$data ['page_title'] = '回款计划 — 昊美科技CRM系统';
		$data ['class4'] = 'cur';
		$data ['all_users'] = $this->get_all_users();
		$query = $this->db->query('SELECT user_name FROM user where id = '.$id.'');
		$row = $query->row();
		$data ['check_user_name'] = $row->user_name;
		$this->load->view ( 'payment_plan_list', $data );
	}

	public function edit($id = ''){
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_plan.*,user.user_name as user_name')->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_plan.owner_id',$user_id);
		}
		$this->db->where('payment_plan.id',$id);
		$query = $this->db->get();
		$data['edit_plans'] = $query->row();
		/*获取所有用户*/
		$this->db->select('*')->from('user');
		$query = $this->db->get();
		$data['users'] = $query->result();
		
		/*获取回款计划期次*/
		$this->db->select ( '*' )->from ( 'payment_plan_stage' )->where ( 'payment_plan_id', $id )->order_by("id", "asc"); 
		$query = $this->db->get ();
		$data['stages'] = $query->result();
		$this->load->view ( 'payment_plan_edit',$data);
	}

	public function update(){
		$data_array = $_POST;
		
		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		$data_array ['customer_id'] = $customer_id;
		
		$stages = $data_array['stages'];
		$money_array=array();
		if($stages == 1){
			if($data_array['money0']){
				$money_array[] = $data_array['money0'];
				unset($data_array['money0']);
			}else{
				$money_array[] = $data_array['money'];
				unset($data_array['money']);
			}
		}else{
			for($i=0;$i<$stages;$i++){
				$money_array[] = $data_array['money'.$i.''];
				unset($data_array['money'.$i.'']);
			}
		}
	
		$stage_time_array = array();
		if($stages == 1){
			if($data_array['stages_payment_time0']){
				$stage_time_array[] = $data_array['stages_payment_time0'];
				unset($data_array['stages_payment_time0']);
			}else{
				$stage_time_array[] = $data_array['stages_payment_time'];
				unset($data_array['stages_payment_time']);
			}
		}else{
			for($i=0;$i<$stages;$i++){
				$stage_time_array[] = $data_array['stages_payment_time'.$i.''];
				unset($data_array['stages_payment_time'.$i.'']);
			}
		}
		
		/*获取合同名称*/
		$this->db->select ( 'subject' )->from ( 'contract' )->where ( 'contract_id', $data_array ['contract_id'] );
		$query = $this->db->get ();
		$row = $query->row();
		$subject = $row->subject;
		$data_array ['contract_name'] = $subject;
		unset($data_array ['customer_name']);

		date_default_timezone_set ( 'PRC' );
		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array['update_time'] = $now_time;

		$id=$data_array ['id'];
		unset($data_array ['id']);

		$this->db->where('id', $id);
		$row=$this->db->update('payment_plan', $data_array);
		if($row > 0){
			$query = $this->db->query("SELECT id FROM payment_plan_stage where payment_plan_id = $id");
			$stage_array=array();
			$this->db->where('payment_plan_id', $id);
			$this->db->delete(payment_plan_stage);
			for ($n=0;$n<$stages;$n++){
					$create_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
					$stage_array['create_time']=$create_time;
					$stage_array['payment_plan_id']=$id;
					$stage_array['money']=$money_array[$n];
					$stage_array['stages_payment_time']=$stage_time_array[$n];
					$this->db->insert('payment_plan_stage', $stage_array);
			}
		}
		redirect ( 'payment_plan/index/' );
	}

	public function show_Payment_record_add(){
		/*获取所有用户*/
		$this->db->select('*')->from('user');
		$query = $this->db->get();
		$data['users'] = $query->result();
		$this->load->view ( 'payment_plan_add',$data);
	}

	public function insert(){ /*添加汇款计划*/
		$data_array = $_POST;
		/*获取这个合同的总金额*/
		$this->db->select ( 'total_amount' )->from ( 'contract' )->where ( 'contract_id', $data_array ['contract_id'] );
		$query = $this->db->get ();
		$row = $query->row();
		$total_amount = $row->total_amount;
		
		$stages = $data_array['stages'];
		$money_array=array();
		if($stages == 1){
			if($data_array['money0']){
				$money_array[] = $data_array['money0'];
				unset($data_array['money0']);
			}else{
				$money_array[] = $data_array['money'];
				unset($data_array['money']);
			}
		}else{
			for($i=0;$i<$stages;$i++){
				$money_array[] = $data_array['money'.$i.''];
				unset($data_array['money'.$i.'']);
			}
		}
		
		$stage_time_array = array();
		if($stages == 1){
			if($data_array['stages_payment_time0']){
				$stage_time_array[] = $data_array['stages_payment_time0'];
				unset($data_array['stages_payment_time0']);
			}else{
				$stage_time_array[] = $data_array['stages_payment_time'];
				unset($data_array['stages_payment_time']);
			}
		}else{
			for($i=0;$i<$stages;$i++){
				$stage_time_array[] = $data_array['stages_payment_time'.$i.''];
				unset($data_array['stages_payment_time'.$i.'']);
			}
		}
		
		$data_array['money'] = $total_amount;
		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		$data_array ['customer_id'] = $customer_id;

		/*获取合同名称*/
		$this->db->select ( 'subject' )->from ( 'contract' )->where ( 'contract_id', $data_array ['contract_id'] );
		$query = $this->db->get ();
		$row = $query->row();
		$subject = $row->subject;
		$data_array ['contract_name'] = $subject;
		unset($data_array ['customer_name']);

		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array['create_time'] = $now_time;
		$q=$this->db->insert('payment_plan', $data_array);
		/*添加回款计划阶段*/
		if($q > 0){
			$contract_id = $data_array['contract_id'];
			$query = $this->db->query("SELECT id FROM payment_plan where contract_id = $contract_id");
			$row = $query->row();
			$payment_plan_id = $row->id;
			$stage_array=array();
			for($n=0;$n<count($money_array);$n++){
				$create_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
				$stage_array['create_time']=$create_time;
				$stage_array['payment_plan_id']=$payment_plan_id;
				$stage_array['money']=$money_array[$n];
				$stage_array['stages_payment_time']=$stage_time_array[$n];
				$q=$this->db->insert('payment_plan_stage', $stage_array);
			}
		}
		redirect ( 'payment_plan/index/' );
	}

	/*
	 * 删除记录
	 */
	public function delete($id = '') {
		$row=$this->db->delete ( 'payment_plan', array ('id' => $id ) );
		if($row > 0){
			$row=$this->db->delete ( 'payment_plan_stage', array ('payment_plan_id' => $id ) );
		}
		redirect ( 'payment_plan/index/' );
	}
	
	/*检测合同是否已经有回款计划*/
	public function check_contract(){
		$data_array = $_POST;
		$contract_id = $data_array['contract_id'];
		$query = $this->db->query("SELECT count(*) as nums FROM payment_plan where contract_id = $contract_id");
		$row = $query->row();
		$nums = $row->nums;
		echo $nums;
	}
	
	/*获取合同的总金额*/
	public function get_contract_money(){
		$data_array = $_POST;
		$contract_id = $data_array['contract_id'];
		$query = $this->db->query("SELECT total_amount as money FROM contract where contract_id = $contract_id");
		$row = $query->row();
		echo $row->money;
	}
	
	/*获取所有用户*/
	public function get_all_users(){
		$query = $this->db->query("SELECT * FROM user order by create_time asc");
		return $query->result();
	}
	
	public function list_user_payment_plans($user_id = 0){ 
		if($user_id == 0){
			redirect ( 'payment_plan/index/' );
		}else{
			$condition = 'contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_plan.*,user.user_name as user_name';
			$this->db->select($condition)->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
			$this->db->where('payment_plan.owner_id',$user_id);
			$this->db->order_by('payment_plan.create_time', "desc");
			$query = $this->db->get();
			$result = $query->result();
			return $result;
		}
	}
}
?>
